/**
 * 
 */
var page = 1;
var line_per_page = 10;
var order = "ASC";
var order_column = "";
var total_row = 0;
var payment_type = 0;
var device = 0;
var payment_partner = 0;
var group = 0;
var time = 0;
var from = "";
var to = "";
/**
 * Thay đổi số trang hiện tại
 * @param new_page
 */
function select_change_page(new_page){
	page = new_page;
}
/**
 * Thay đổi số dòng hiển thị trên một trang
 * @param new_line
 */
function select_change_line(new_line){
	line_per_page = new_line;
	var max_page = Math.ceil(total_row/line_per_page);
	if (page > max_page) {
		page = max_page;
	}
}
/**
 * Thay đổi trường sắp xếp
 * @param new_order
 */
function select_change_order(new_order){
	if (order_column == new_order) {
		if (order == "ASC") {
			order = "DESC";
		}else{
			order = "ASC";
		}
	}else{
		order_column = new_order;
	}
}
/**
 * Gán giá trị số dòng tối đa
 * @param row
 */
function select_set_total_row(row) {
	total_row = row;
}
/**
 * Gán dữ liệu cần truyền để lọc
 * @param row
 */
function set_loai_hinh(item){
    payment_type = item;
}
function set_thiet_bi(item){
    device = item;
}
function set_doi_tac_tt(item){
    payment_partner = item;
}
function set_group(item){
    group = item;
}
function set_time(item){
    time = item;
}
function set_from(item){
    from = item;

}
function set_to(item){
    to = item;
}
/**
 * Load phân trang
 */
function select_paging(id,url){
	if (total_row > line_per_page) {
		var max_page = Math.ceil(total_row/line_per_page);
		var str = "<ul>";
		if (page > 2) {
			str += "<li><a href='#' onclick = select_change_page(1);select_refreshTable('" + id + "','" + url + "')>First</a></li>";
			str += "<li><a href='#' onclick = select_change_page(" + (page - 1) + ");select_refreshTable('" + id + "','" + url + "')>Prev</a></li>";
			str += "<li><a href='#' onclick = select_change_page(" + (page - 2) + ");select_refreshTable('" + id + "','" + url + "')>" + (page - 2) + "</a></li>";
			str += "<li><a href='#' onclick = select_change_page(" + (page - 1) + ");select_refreshTable('" + id + "','" + url + "')>" + (page - 1) + "</a></li>";
		}else if (page > 1) {
			str += "<li><a href='#' onclick = select_change_page(1);refreshTable('" + id + "','" + url + "')>First</a></li>";
			str += "<li><a href='#' onclick = select_change_page(" + (page - 1) + ");select_refreshTable('" + id + "','" + url + "')>Prev</a></li>";
			str += "<li><a href='#' onclick = select_change_page(" + (page - 1) + ");select_refreshTable('" + id + "','" + url + "')>" + (page - 1) + "</a></li>";
		}else{
			str += "<li class='disabled'><a href='#'>First</a></li>";
			str += "<li class='disabled'><a href='#'>Prev</a></li>";
		}
		str += "<li class='active'><a href='#'>" + page + " / " + max_page + "</a></li>";
		if (page + 1 < max_page) {
			str += "<li><a href='#' onclick = select_change_page(" + (page + 1) + ");select_refreshTable('" + id + "','" + url + "')>" + (page + 1) + "</a></li>";
			str += "<li><a href='#' onclick = select_change_page(" + (page + 2) + ");select_refreshTable('" + id + "','" + url + "')>" + (page + 2) + "</a></li>";
			str += "<li><a href='#' onclick = select_change_page(" + (page + 1) + ");select_refreshTable('" + id + "','" + url + "')>Next</a></li>";
			str += "<li><a href='#' onclick = select_change_page(" + max_page + ");select_refreshTable('" + id + "','" + url + "')>Last</a></li>";
		}else if (page < max_page) {
			str += "<li><a href='#' onclick = select_change_page(" + (page + 1) + ");select_refreshTable('" + id + "','" + url + "')>" + (page + 1) + "</a></li>";
			str += "<li><a href='#' onclick = select_change_page(" + (page + 1) + ");select_refreshTable('" + id + "','" + url + "')>Next</a></li>";
			str += "<li><a href='#' onclick = select_change_page(" + max_page + ");select_refreshTable('" + id + "','" + url + "')>Last</a></li>";
		}else{
			str += "<li class='disabled'><a href='#'>Next</a></li>";
			str += "<li class='disabled'><a href='#'>Last</a></li>";
		}
		$(".pagination").html(str);
	}else{
		$(".pagination").html("");
	}
//	 <ul>
//	 <li><a href="#">Prev</a></li>
//	 <li><a href="#">1</a></li>
//	 <li><a href="#">2</a></li>
//	 <li><a href="#">3</a></li>
//	 <li><a href="#">4</a></li>
//	 <li><a href="#">Next</a></li>
//	 </ul>
}
/**
 * Load nội dung vào bảng
 * @param id id của thẻ html sẽ load nội dung
 * @param url url load Ajax
 */
function select_refreshTable(id,url){
	$.ajax({
		  url: url,
		  type: "POST",
		  data: {'page':page,'line_per_page':line_per_page,'order':order,'order_column' : order_column,
          'payment_type': payment_type, 'device': device, 'payment_partner': payment_partner, 'group': group, 'time': time,
          'from': from, 'to': to},
		  beforeSend : function() {
			showLoading();
		  },
		  success: function(data) {
			  $('#' + id).html(data);
		  },
		  complete: function(){	
			  select_paging(id,url);
			  hiddenLoading();
		  },
		  error: function() {
//			  $("#error-dialog").dialog( "destroy" );
			  $("#error-dialog").html('Có lỗi khi load dữ liệu từ server. Bạn vui lòng thử lại sau');
			  $("#error-dialog").dialog({
					resizable: false,
					height:140,
					modal: true
				});
		  }
	});
}